<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="参考：https://blog.jianchihu.net/mp4-elst-box.html
一、tkhd(Track Header Box)首先看一下软件解析出来的情况：
下面看一下box的结构体表示：12345678910111213141516171819202122232425262728">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="MP4格式解析三 trak-&gt;tkhd &amp; edts/elst">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="参考：https://blog.jianchihu.net/mp4-elst-box.html
一、tkhd(Track Header Box)首先看一下软件解析出来的情况：
下面看一下box的结构体表示：12345678910111213141516171819202122232425262728">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>MP4格式解析三 trak-&gt;tkhd &amp; edts/elst - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>MP4格式解析三 trak->tkhd & edts/elst</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2019-08-14
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        


<a href="/blog/tags/MP4/">#MP4</a>


                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                        

<a href="/blog/categories/音视频/">音视频</a>

                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>参考：<a href="https://blog.jianchihu.net/mp4-elst-box.html" target="_blank" rel="noopener">https://blog.jianchihu.net/mp4-elst-box.html</a></p>
<h3 id="一、tkhd-Track-Header-Box"><a href="#一、tkhd-Track-Header-Box" class="headerlink" title="一、tkhd(Track Header Box)"></a>一、tkhd(Track Header Box)</h3><p>首先看一下软件解析出来的情况：<br><img src="http://cyrus_huang.gitee.io/blog/2019/08/14/MP4格式解析三-trak-tkhd-edts-elst/tkhd_1.png" alt="音频track header box"><br><img src="http://cyrus_huang.gitee.io/blog/2019/08/14/MP4格式解析三-trak-tkhd-edts-elst/tkhd_2.png" alt="视频track header box"></p>
<p>下面看一下box的结构体表示：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">tkhd(Track Header Box)</span><br><span class="line">// tkhd box  </span><br><span class="line">typedef struct &#123;  </span><br><span class="line">    u_char    size[4];  </span><br><span class="line">    u_char    name[4];  </span><br><span class="line">    u_char    version[1];  </span><br><span class="line">    /* </span><br><span class="line">        flags标志位 </span><br><span class="line">        按位或操作结果值，预定义如下： </span><br><span class="line">        0x000001 track_enabled，否则该track不被播放； </span><br><span class="line">        0x000002 track_in_movie，表示该track在播放中被引用； </span><br><span class="line">        0x000004 track_in_preview，表示该track在预览时被引用。 </span><br><span class="line">        一般该值为7，如果一个媒体所有track均未设置track_in_movie和track_in_preview， </span><br><span class="line">        将被理解为所有track均设置了这两项；对于hint track，该值为0 </span><br><span class="line">    */  </span><br><span class="line">    u_char    flags[3];  </span><br><span class="line">    u_char    creation_time[4];  </span><br><span class="line">    u_char    modification_time[4];  </span><br><span class="line">    u_char    track_id[4]; // id号，不能重复且不能为0  </span><br><span class="line">    u_char    reserved1[4];  </span><br><span class="line">    u_char    duration[4]; // track的时间长度  </span><br><span class="line">    u_char    reserved2[8];  </span><br><span class="line">    u_char    layer[2]; // 视频层，默认为0，值小的在上层  </span><br><span class="line">    u_char    group[2]; // track分组信息，默认为0表示该track未与其他track有群组关系  </span><br><span class="line">    u_char    volume[2]; // 音量  </span><br><span class="line">    u_char    reverved3[2];  </span><br><span class="line">    u_char    matrix[36]; // 视频变换矩阵  </span><br><span class="line">    u_char    width[4]; // 宽  </span><br><span class="line">    u_char    heigth[4]; // 高  </span><br><span class="line">&#125; mp4_tkhd_atom;</span><br></pre></td></tr></table></figure></p>
<p>看一下具体的数据：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">音频tkhd</span><br><span class="line"></span><br><span class="line">00 00 00 5C 74 6B 68 64 00 00 00 01 D9 70 A8 7B ; ...\tkhd.....p.&#123;</span><br><span class="line">D9 70 A8 7B 00 00 00 01 00 00 00 00 00 00 DA BF ; .p.&#123;............</span><br><span class="line">00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ; ................</span><br><span class="line">00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................</span><br><span class="line">00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................</span><br><span class="line">40 00 00 00 00 00 00 00 00 00 00 00             ; @...........</span><br><span class="line"></span><br><span class="line">=====================  解析  =====================</span><br><span class="line">00 00 00 5C: 长度92</span><br><span class="line">74 6B 68 64: tkhd</span><br><span class="line">00: version0</span><br><span class="line">00 00 01: flag 1</span><br><span class="line">D9 70 A8 7B:  creation_time (2019/08/07 16:10:35)</span><br><span class="line">D9 70 A8 7B:  modifyTime</span><br><span class="line">00 00 00 01: track id 1</span><br><span class="line">00 00 00 00: reverse</span><br><span class="line">00 00 DA BF: duration (55999) / time base(6000) = 9.33s</span><br><span class="line">00 00 00 00 00 00 00 00: reverse2</span><br><span class="line">00 00: layer, 视频层，默认为0，值小的在上层  </span><br><span class="line">00 00: group, track分组信息，默认为0表示该track未与其他track有群组关系</span><br><span class="line">00 00: 音量  </span><br><span class="line">00 00:  reverse</span><br><span class="line">00 01 00 00  00 00 00 00   00 00 00 00  </span><br><span class="line">00 00 00 00  00 01 00 00   00 00 00 00 </span><br><span class="line">00 00 00 00  00 00 00 00   40 00 00 00: matrix[36]; // 视频变换矩阵</span><br><span class="line">00 00 00 00: width 0</span><br><span class="line">00 00 00 00: height 0</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">视频tkhd</span><br><span class="line">00 00 00 5C 74 6B 68 64 00 00 00 01 D9 70 A8 7B ; ...\tkhd.....p.&#123;</span><br><span class="line">D9 70 A8 7B 00 00 00 02 00 00 00 00 00 00 DA C0 ; .p.&#123;............</span><br><span class="line">00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ; ................</span><br><span class="line">00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................</span><br><span class="line">00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................</span><br><span class="line">40 00 00 00 05 12 00 00 03 C6 00 00             ; @...........</span><br><span class="line"></span><br><span class="line">=====================  解析  =====================</span><br><span class="line">前面跟音频一样的，只看最后8字节的宽高解析，应该也是[16.16]的结构，即前16个字节为整数部分大小，后16字节为小数大小</span><br><span class="line"></span><br><span class="line">05 12 00 00: 1298.0 (0X05120000&gt;&gt;16)</span><br><span class="line">03 C6 00 00: 966.0 (0X03C60000&gt;&gt;16)</span><br></pre></td></tr></table></figure>
<h3 id="二、elst-Edit-List-Box"><a href="#二、elst-Edit-List-Box" class="headerlink" title="二、elst(Edit List Box)"></a>二、elst(Edit List Box)</h3><p>首先看一下elst(Edit List Box)的解析伪代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">aligned(8) class EditListBox extends FullBox(‘elst’, version, 0) &#123;</span><br><span class="line">   unsigned int(32)  entry_count;</span><br><span class="line">   for (i=1; i &lt;= entry_count; i++) &#123;</span><br><span class="line">        if (version==1) &#123;</span><br><span class="line">           unsigned int(64) segment_duration;</span><br><span class="line">           int(64) media_time;</span><br><span class="line">        &#125; else &#123; // version==0</span><br><span class="line">           unsigned int(32) segment_duration;</span><br><span class="line">           int(32)  media_time;</span><br><span class="line">        &#125;</span><br><span class="line">            int(16) media_rate_integer;</span><br><span class="line">            int(16) media_rate_fraction = 0;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>再看两个实际数据：<br><img src="http://cyrus_huang.gitee.io/blog/2019/08/14/MP4格式解析三-trak-tkhd-edts-elst/edts_1.png" alt=""><br>看一下具体的数据：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">00 00 00 24 65 64 74 73 00 00 00 1C 65 6C 73 74 ; ...$edts....elst</span><br><span class="line">00 00 00 00 00 00 00 01 00 00 DA C0 00 00 01 90 ; ................</span><br><span class="line">00 01 00 00    </span><br><span class="line"></span><br><span class="line">=====================  解析  =====================</span><br><span class="line">00 00 00 24: 长度36</span><br><span class="line">65 64 74 73: edts</span><br><span class="line">00 00 00 1C: 长度28</span><br><span class="line">65 6C 73 74: elst</span><br><span class="line">00: version 0</span><br><span class="line">00 00 00 00: flag 0</span><br><span class="line">00 00 00 01: Entry count 1</span><br><span class="line">   00 00 DA C0: Segment duration 56000</span><br><span class="line">   00 00 01 90: Media time 400</span><br><span class="line">   00 01: media_rate_integer 1</span><br><span class="line">   00 00: Media rate fraction 0</span><br><span class="line">   </span><br><span class="line">elst实例参数解析：</span><br><span class="line">segment_duration: 表示该edit段的时长，以Movie Header Box（mvhd）中的timescale为单位,即 segment_duration/timescale = 实际时长（单位s）</span><br><span class="line"></span><br><span class="line">media_time: 表示该edit段的起始时间，以track中Media Header Box（mdhd）中的timescale为单位。如果值为-1(FFFFFF)，表示是空edit，一个track中最后一个edit不能为空。</span><br><span class="line"></span><br><span class="line">media_rate: edit段的速率为0的话，edit段相当于一个”dwell”，即画面停止。画面会在media_time点上停止segment_duration时间。否则这个值始终为1。</span><br><span class="line"></span><br><span class="line">* 即上面的elst实例表示的含义为：从400/timescale 开始，以正常速率播放至结束</span><br></pre></td></tr></table></figure></p>
<p><img src="http://cyrus_huang.gitee.io/blog/2019/08/14/MP4格式解析三-trak-tkhd-edts-elst/edts_2.png" alt=""><br>看一下具体的数据：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">00 00 00 30 65 64 74 73 00 00 00 28 65 6C 73 74 ; ...0edts...(elst</span><br><span class="line">00 00 00 00 00 00 00 02 00 00 08 2A FF FF FF FF ; ...........*....</span><br><span class="line">00 01 00 00 00 00 D2 95 00 00 00 00 00 01 00 00 ; ................</span><br><span class="line"></span><br><span class="line">=====================  解析  =====================</span><br><span class="line">00 00 00 30: 长度48</span><br><span class="line">00 00 00 28: 长度40</span><br><span class="line">65 6C 73 74: elst</span><br><span class="line">00: version 0</span><br><span class="line">00 00 00 00: flag 0</span><br><span class="line">00 00 00 02: Entry count 2</span><br><span class="line">   00 00 08 2A: Segment duration 2090</span><br><span class="line">   FF FF FF FF: Media time -1</span><br><span class="line">   00 01: media_rate_integer 1</span><br><span class="line">   00 00: Media rate fraction 0</span><br><span class="line">   		* 该Entry表示，从开始到2090/timebase不播放该trak,即该trak整体延迟2090/timebase播放</span><br><span class="line">        </span><br><span class="line">   00 00 D2 95： Segment duration 53909</span><br><span class="line">   00 00 00 00： Media time 0</span><br><span class="line">   00 01: media_rate_integer 1</span><br><span class="line">   00 00: Media rate fraction 0</span><br><span class="line">   		* 该Entry表示，从2090/timebase开始到结束，正常播放</span><br></pre></td></tr></table></figure></p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>